package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentMapper studentMapper;


    /**
     * 条件分页查询学员
     *
     * @param student 学院对象
     * @return 分页查询结果
     */
    @Override
    public PageResult<Student> getStudentByPage(StudentQueryParam student) {
        PageResult<Student> pageResult = new PageResult<>();

        //利用page方法完成分页
        Page<Student> page = new Page<>(student.getPage(), student.getPageSize());

        page.addOrder(OrderItem.desc("update_time"));

        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        page = page(page);

        //根据学生对象中的中的clazzId查询出班级名称返回给学生对象
        List<Student> students = page.getRecords();
        List<Long> clazzIds = students.stream().map(Student::getClazzId).toList();

        if (!clazzIds.isEmpty()) {
            List<Clazz> clazzList = clazzService.listByIds(clazzIds);
            clazzList.forEach(clazz -> {
                students.forEach(stu -> {
                    if (stu.getClazzId().equals(clazz.getId())) {
                        stu.setClazzName(clazz.getName());
                    }
                });
            });
        }

        //返回分页结果
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(students);
        return pageResult;
    }

    /**
     * 新增学员
     *
     * @param student 学员对象
     */
    @Override
    public void saveStu(Student student) {

        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
//        if (student.getClazzId() == null) {
//            student.setClazzId(null);
//            student.setClazzName(null);
//        }
        save(student);

    }

    /**
     * 修改学员
     *
     * @param student 学员对象
     */
    @Override
    public void updateStu(Student student) {

        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    /**
     * 违纪扣分
     *
     * @param id    学员id
     * @param score 扣分的分值
     */
    @Override
    public void updateViolById(Integer id, Short score) {

        Student stu = getById(id);

        stu.setViolationScore(score);
        stu.setViolationCount((short) (stu.getViolationCount() + 1));
        stu.setUpdateTime(LocalDateTime.now());

        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getId, id);

        update(stu, wrapper);
    }

    /**
     * 统计班级人数
     *
     * @return 各个班级的人数
     */
    @Override
    public ClazzInfo getClazzInfo() {
        ClazzInfo clazzInfo = new ClazzInfo();
        List<Map<String,Object>> list = studentMapper.getClazzInfo();

        //从数据库返回值中获取到clazzList和dataList
        List<Object> names = list.stream().map(clazz -> clazz.get("name")).toList();
        List<Object> count = list.stream().map(num -> num.get("value")).toList();
        //封装到clazzInfo对象中
        clazzInfo.setClazzList(names);
        clazzInfo.setDataList(count);
        return clazzInfo;
    }

    /**
     * 统计各个学历的人数
     *
     * @return 各个学历的人数集合
     */
    @Override
    public List<Map<String, Object>> getDegreeInfo() {
        List<Map<String, Object>> degreeInfo = studentMapper.getBegreeIngo();
        return degreeInfo;
    }
}
